package com.cloud.auth.controller;

import com.cloud.auth.service.BaseService;
import com.cloud.auth.service.RoleService;
import com.cloud.auth.service.UserRoleService;
import com.cloud.auth.util.ApiResult;
import com.cloud.auth.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @packageName: com.api.controller.auth
 * @name: UserRoleController
 * @description:
 * @author: 董定卓
 * @dateTime: 2019/6/19 10:22
 */
@RestController
@RequestMapping("/userRole")
public class UserRoleController extends BaseController {

    @Autowired
    UserRoleService service;

    @Autowired
    RoleService roleService;

    @Override
    protected BaseService getService() {
        return service;
    }


    /**
     * 绑定用户角色
     *
     * @param map
     * @return
     */
    @PostMapping("/add")
    public ApiResult add(@RequestParam Map<String, Object> map) {
        if (MapUtil.getString(map, "userId") == null) {
            return ApiResult.failResult("用户ID不能为空");
        }
        int i = service.add(map);
        if (i > 0) {
            return ApiResult.successResult();
        }
        return ApiResult.failResult("添加失败");
    }

    /**
     * 用户角色
     *
     * @param map
     * @return
     */
    @PostMapping("/list")
    public ApiResult list(@RequestParam Map<String, Object> map) {
        if (MapUtil.getString(map, "userId") == null) {
            return ApiResult.failResult("用户ID不能为空");
        }

        List<Map<String, Object>> allRole = roleService.simpleList(null);
        List<String> ownRole = service.ownRole(map);

        Map<String, Object> result = new HashMap<>();
        result.put("allRole", allRole);
        result.put("ownRole", ownRole);

        return ApiResult.successResultForData(result);
    }
}
